﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.GrippedObjectList.CUiSetGrippedObjectsForm
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Forms;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui;
using Tecnomatix.Planning;


namespace DnProcessSimulateCommands.GrippedObjectList;

public class CUiSetGrippedObjectsForm : TxForm
{
  private bool _isLoaded;
  private TxGripper _gripper;
  private IContainer components;
  private TxObjGridCtrl _grippedObjectsGrid;
  private Label label1;
  private RadioButton _allObjectsRadioButton;
  private RadioButton _grippedObjectsRadioButton;
  private Button _okButton;
  private Button _cancelButton;

  public CUiSetGrippedObjectsForm()
  {
    this.InitializeComponent();
    this.InitializeGridLayout();
    this.RegisterForDocumentUnloading();
  }

  private void InitializeGridLayout()
  {
    TxGridColumnData txGridColumnData = new TxGridColumnData();
    txGridColumnData.Init(StringTable.DEFINED_OBJECTS_HEADER, (TxGridColumnType) 0);
    TxGridColumnDataCollection columnDataCollection = new TxGridColumnDataCollection();
    columnDataCollection.AddItem(txGridColumnData);
    TxGridDescription txGridDescription = new TxGridDescription();
    txGridDescription.SetData(columnDataCollection, 0);
    this._grippedObjectsGrid.Init(txGridDescription);
  }

  private void RegisterForDocumentUnloading()
  {
    TxApplication.ActiveDocument.Unloading += new TxDocument_UnloadingEventHandler(this.OnDocumentUnloading);
  }

  private void UnRegisterFromDocumentUnloading()
  {
    TxApplication.ActiveDocument.Unloading -= new TxDocument_UnloadingEventHandler(this.OnDocumentUnloading);
  }

  private void InitializeRadioButtons()
  {
    if (this._gripper.UseDefinedObjectsAllowedToBeGripped)
      this._grippedObjectsRadioButton.Checked = true;
    else
      this._allObjectsRadioButton.Checked = true;
  }

  private void InitializeGrid()
  {
    this.SetGridValidator();
    this.LoadGrippedObjectsInGrid();
    this.SetGridState();
  }

  private void SetGridValidator()
  {
    this._grippedObjectsGrid.SetValidator((ITxValidator) new CApSetGripppedObjectsGridValidator(this._gripper, this._grippedObjectsGrid));
  }

  private void SetGridState()
  {
    bool flag = this._grippedObjectsRadioButton.Checked;
    if (!flag && ((Control) this._grippedObjectsGrid).Enabled && this._grippedObjectsGrid.Count > 0)
      this._grippedObjectsGrid.SetCurrentCell(0, 0);
    ((Control) this._grippedObjectsGrid).Enabled = flag;
    if (!flag)
      return;
    this._grippedObjectsGrid.Focus();
  }

  private void LoadGrippedObjectsInGrid()
  {
    TxObjectList allowedToBeGripped = this._gripper.ObjectsAllowedToBeGripped;
    if (allowedToBeGripped == null)
      return;
    foreach (ITxObject itxObject in (Collection<ITxObject>) allowedToBeGripped)
    {
      if (itxObject is ITxPlanningObject && CApSetGripppedObjectsUtils.IsValidObjectForUI(itxObject))
        this._grippedObjectsGrid.AppendObject(itxObject);
    }
  }

  private void CUiSetGrippedObjectsForm_Load(object sender, EventArgs e)
  {
    this._gripper = CApSetGripppedObjectsUtils.GetGripperFromSelection();
    ((Control) this).Text = string.Format(StringTable.DIALOG_NAME, (object) ((TxBaseGeometryCreationObject) this._gripper).Name);
    this.InitializeRadioButtons();
    this.InitializeGrid();
    this._isLoaded = true;
  }

  private void _grippedObjectsRadioButton_CheckedChanged(object sender, EventArgs e)
  {
    if (!this._isLoaded)
      return;
    this.SetGridState();
  }

  private void _okButton_Click(object sender, EventArgs e)
  {
    TxApplication.ActiveUndoManager.StartTransaction();
    this._gripper.ObjectsAllowedToBeGripped = new TxObjectList(this._grippedObjectsGrid.Objects);
    this._gripper.UseDefinedObjectsAllowedToBeGripped = this._grippedObjectsRadioButton.Checked;
    TxApplication.ActiveUndoManager.EndTransaction();
    ((Form) this).Close();
  }

  private void _cancelButton_Click(object sender, EventArgs e) => ((Form) this).Close();

  private void CUiSetGrippedObjectsForm_FormClosing(object sender, FormClosingEventArgs e)
  {
    this.UnRegisterFromDocumentUnloading();
  }

  private void _grippedObjectsGrid_ObjectInserted(
    object sender,
    TxObjGridCtrl_ObjectInsertedEventArgs args)
  {
    ITxPlanningObject planningPart = CApSetGripppedObjectsUtils.GetPlanningPart(args.Obj);
    this._grippedObjectsGrid.SetObject(args.Row, (ITxObject) planningPart);
  }

  private void OnDocumentUnloading(object sender, TxDocument_UnloadingEventArgs args)
  {
    ((Form) this).Close();
  }

  protected virtual void Dispose(bool disposing)
  {
    if (disposing && this.components != null)
      this.components.Dispose();
    base.Dispose(disposing);
  }

  private void InitializeComponent()
  {
    ComponentResourceManager componentResourceManager = new ComponentResourceManager(typeof (CUiSetGrippedObjectsForm));
    this._grippedObjectsGrid = new TxObjGridCtrl();
    this.label1 = new Label();
    this._allObjectsRadioButton = new RadioButton();
    this._grippedObjectsRadioButton = new RadioButton();
    this._okButton = new Button();
    this._cancelButton = new Button();
    ((Control) this).SuspendLayout();
    componentResourceManager.ApplyResources((object) this._grippedObjectsGrid, "_grippedObjectsGrid");
    ((UserControl) this._grippedObjectsGrid).BorderStyle = BorderStyle.FixedSingle;
    this._grippedObjectsGrid.ChangeGeneralSelection = false;
    this._grippedObjectsGrid.CurrentRow = -1;
    this._grippedObjectsGrid.EnableMultipleSelection = true;
    this._grippedObjectsGrid.EnableRecurringObjects = false;
    this._grippedObjectsGrid.ListenToPick = true;
    ((Control) this._grippedObjectsGrid).Name = "_grippedObjectsGrid";
    this._grippedObjectsGrid.PickLevel = (TxPickLevel) 1;
    this._grippedObjectsGrid.ReadOnly = false;
    this._grippedObjectsGrid.ValidatorType = (TxValidatorType) 1;
    this._grippedObjectsGrid.ObjectInserted += new TxObjGridCtrl_ObjectInsertedEventHandler(this._grippedObjectsGrid_ObjectInserted);
    componentResourceManager.ApplyResources((object) this.label1, "label1");
    this.label1.Name = "label1";
    componentResourceManager.ApplyResources((object) this._allObjectsRadioButton, "_allObjectsRadioButton");
    this._allObjectsRadioButton.Name = "_allObjectsRadioButton";
    this._allObjectsRadioButton.TabStop = true;
    this._allObjectsRadioButton.UseVisualStyleBackColor = true;
    componentResourceManager.ApplyResources((object) this._grippedObjectsRadioButton, "_grippedObjectsRadioButton");
    this._grippedObjectsRadioButton.Name = "_grippedObjectsRadioButton";
    this._grippedObjectsRadioButton.TabStop = true;
    this._grippedObjectsRadioButton.UseVisualStyleBackColor = true;
    this._grippedObjectsRadioButton.CheckedChanged += new EventHandler(this._grippedObjectsRadioButton_CheckedChanged);
    componentResourceManager.ApplyResources((object) this._okButton, "_okButton");
    this._okButton.Name = "_okButton";
    this._okButton.UseVisualStyleBackColor = true;
    this._okButton.Click += new EventHandler(this._okButton_Click);
    componentResourceManager.ApplyResources((object) this._cancelButton, "_cancelButton");
    this._cancelButton.Name = "_cancelButton";
    this._cancelButton.UseVisualStyleBackColor = true;
    this._cancelButton.Click += new EventHandler(this._cancelButton_Click);
    componentResourceManager.ApplyResources((object) this, "$this");
    ((ContainerControl) this).AutoScaleMode = AutoScaleMode.Font;
    ((Control) this).Controls.Add((Control) this._cancelButton);
    ((Control) this).Controls.Add((Control) this._okButton);
    ((Control) this).Controls.Add((Control) this._grippedObjectsRadioButton);
    ((Control) this).Controls.Add((Control) this._allObjectsRadioButton);
    ((Control) this).Controls.Add((Control) this.label1);
    ((Control) this).Controls.Add((Control) this._grippedObjectsGrid);
    ((Form) this).MaximizeBox = false;
    ((Form) this).MinimizeBox = false;
    ((Control) this).Name = "CUiSetGrippedObjectsListForm";
    ((Form) this).ShowIcon = false;
    ((Form) this).FormClosing += new FormClosingEventHandler(this.CUiSetGrippedObjectsForm_FormClosing);
    ((Form) this).Load += new EventHandler(this.CUiSetGrippedObjectsForm_Load);
    ((Control) this).ResumeLayout(false);
    ((Control) this).PerformLayout();
  }
}
